package com.oro.backingbeans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.oro.domain.Categoria;
import com.oro.domain.Scuola;
import com.oro.service.StudentService;

public class SearchSchoolBean implements ActionListener {

	private static final Logger logger = Logger
			.getLogger(SearchStudentBean.class);

	public static Logger getLogger() {
		return logger;
	}

	private List<SelectItem> categoryOptions;
	private Long courseCategoryId;
	private float maxPrice;
	private float minPrice;
	private String schoolName;

	private List<String> schoolNames;
	private String schoolTown;
	private List<Scuola> searchSchoolResult;

	private boolean showResults;

	@Autowired
	StudentService studentService;

	private List<SelectItem> townOptions;

	public SearchSchoolBean() {
		super();
	}

	public List<String> completeSchoolName(String query) {
		List<String> results = new ArrayList<String>();
		int max = 10;
		int j = 0;
		String temp;
		while (results.size() < max && j < schoolNames.size()) {
			temp = schoolNames.get(j++);
			if (temp.startsWith(query)) {
				results.add(temp);
			}
		}
		return results;
	}

	public List<SelectItem> getCategoryOptions() {
		categoryOptions = new ArrayList<SelectItem>();
		categoryOptions.add(new SelectItem(null, ""));
		for (Categoria c : studentService.getCategories()) {
			categoryOptions.add(new SelectItem(c.getId(), c.getName()));
		}
		return categoryOptions;
	}

	public Long getCourseCategoryId() {
		return courseCategoryId;
	}

	public float getMaxPrice() {
		return maxPrice;
	}

	public float getMinPrice() {
		return minPrice;
	}

	public String getSchoolName() {
		return schoolName;
	}

	public List<String> getSchoolNames() {
		return schoolNames;
	}

	public String getSchoolTown() {
		return schoolTown;
	}

	public List<Scuola> getSearchSchoolResult() {
		return searchSchoolResult;
	}

	public List<SelectItem> getTownOptions() {
		townOptions = new ArrayList<SelectItem>();
		townOptions.add(new SelectItem("", ""));
		for (String town : studentService.getTownsAu()) {
			if (town != null && town != "")
				townOptions.add(new SelectItem(town, town));
		}
		return townOptions;
	}

	public boolean isShowResults() {
		return showResults;
	}

	@PostConstruct
	public void postConstruct() {
		// Generate the list of school names, for autocomplete
		schoolNames = studentService.getSchoolsNames();

		searchSchoolResult = new ArrayList<Scuola>();

		getLogger().info("SearchSchoolBean has been constructed");
	}

	@PreDestroy
	public void preDestroy() {
		getLogger().info("SearchSchoolBean is being destroyed.");
	}

	@Override
	public void processAction(ActionEvent e) throws AbortProcessingException {
		if (e.getComponent().getId().equals("searchMenuitem")) {
			reset();
		}
	}

	public void reset() {
		schoolName = null;
		schoolTown = null;
		courseCategoryId = 0L;
		minPrice = 0;
		maxPrice = 0;
		searchSchoolResult = new ArrayList<Scuola>();
	}

	public void setCategoryOptions(List<SelectItem> categoryOptions) {
		this.categoryOptions = categoryOptions;
	}

	public void setCourseCategoryId(Long courseCategoryId) {
		this.courseCategoryId = courseCategoryId;
	}

	public void setMaxPrice(float maxPrice) {
		this.maxPrice = maxPrice;
	}

	public void setMinPrice(float minPrice) {
		this.minPrice = minPrice;
	}

	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}

	public void setSchoolNames(List<String> schoolNames) {
		this.schoolNames = schoolNames;
	}

	public void setSchoolTown(String schoolTown) {
		this.schoolTown = schoolTown;
	}

	public void setSearchSchoolResult(List<Scuola> searchSchoolResult) {
		this.searchSchoolResult = searchSchoolResult;
	}

	public void setShowResults(boolean showResults) {
		this.showResults = showResults;
	}

	public void setTownOptions(List<SelectItem> townOptions) {
		this.townOptions = townOptions;
	}

	public List<Scuola> submit() {
		searchSchoolResult = studentService.searchSchools(schoolName,
				schoolTown, courseCategoryId, minPrice, maxPrice);

		schoolName = null;
		schoolTown = null;
		courseCategoryId = 0L;
		minPrice = 0;
		maxPrice = 0;

		return searchSchoolResult;
	}

}
